import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    
    let controller: FlutterViewController  = window.rootViewController as! FlutterViewController
    let channel = FlutterMethodChannel(name: "codewhy.com/bettery", binaryMessenger: controller.binaryMessenger);
    channel.setMethodCallHandler { call, result in
        if call.method == "getBetteryInfo" {
            result(FlutterMethodNotImplemented)
            return
        }
        
        let device = UIDevice.current
        device.isBatteryMonitoringEnabled = true
        
        if device.batteryState == UIDevice.BatteryState.unknown{
            result(FlutterError(code: "unknow", message: "Battery info is unknow", details: nil))
        }else{
            result(Int(device.batteryLevel * 100))
        }
        
    }
    
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
